﻿#region C# Libraries

using System.Collections.Generic;

#endregion

#region XNA Libraries

using Microsoft.Xna.Framework;

#endregion

#region SDK Libraries

using main.sdk.audio;
using main.sdk.display;
using main.sdk.display.object2d;
using main.sdk.input;
using Microsoft.Xna.Framework.Input;
using main.sdk.utils;
using FarseerPhysics.Dynamics;
using FarseerPhysics.Factories;

#endregion

namespace main.src.game
{
    /**
     * Classe que controla a vara ("ui")
     */
    public class Game_Rod
    {
        #region Public Variables

        public readonly Game_Team ParentTeam = null;
        public readonly Body      Body = null;

        #endregion

        #region Private Variables

        protected Sprite            _RodSprite = new Sprite("RSC/Picture/Bar");
        protected List<Game_Player> _PlayerList = new List<Game_Player>();
        private   Counter           _MoveCounter = new Counter();
        private   float             _TargetMoveSpeed = 0;

        #endregion

        #region Public Members

        /**
         * Construtor do Game_Rod.
         */
        protected Game_Rod(Game_Team Parent, float XPosition)
        {
            ParentTeam = Parent;

            Body                     = BodyFactory.CreateBody(ParentTeam.ParentField.World, Vector2.Multiply(new Vector2(XPosition, 42), 1.0f / Parent.ParentField.Scale));
            Body.BodyType            = BodyType.Kinematic;
            Body.CollisionCategories = Category.All;
            Body.CollidesWith        = Category.All;

            /* Set the rod position. */
            _RodSprite.X = XPosition;
            _RodSprite.Y = 42;
            _RodSprite.OX = _RodSprite.Width / 2;
            _RodSprite.OY = 0;
        }

        /**
         * Adiciona um jogador.
         */
        public void AddPlayer(float Relative)
        {
            /* Atualiza a posição da vara. */
            _PlayerList.Add(new Game_Player(this, Relative));
        }

        /**
         * Atualiza a vara
         */
        public virtual void Update(double Time)
        {
            /* Atualiza os jogadores */
            foreach (Game_Player Player in _PlayerList)
            {
                Player.Update(Time);
            }

            

            _MoveCounter.Update(Time);
            Body.LinearVelocity = new Vector2(0, (float)_MoveCounter.Value);
        }

        /**
         * Move a vara.
         */
        public void SetMoveSpeed(float Speed, double Time = 0.05)
        {
            if (_TargetMoveSpeed != Speed)
            {
                /* Atualiza os jogadores */
                _MoveCounter.Reset(_MoveCounter.Value, Speed, Time, 2);
                
                /* */
                _TargetMoveSpeed = Speed;
            }
        }

        #endregion
    }
}